package com.bawei.week.sing

import com.alibaba.android.arouter.facade.annotation.Route
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MapView
import com.baidu.mapapi.map.MyLocationData
import com.bawei.lib_base.mvvm.BaseActivity
import com.bawei.week.databinding.ActivitySingBinding
import dagger.hilt.android.AndroidEntryPoint


@Route(path = "/app/SingActivity")
@AndroidEntryPoint
class SingActivity : BaseActivity<ActivitySingBinding,SingViewModel>() {
    lateinit var mMapView: MapView
    lateinit var mBaiduMap : BaiduMap
    lateinit var mLocationClient : LocationClient
    override fun initView() {
        mMapView = binding.bmapView

        mBaiduMap = mMapView.map

        initMap()
    }

    override fun initData() {

    }

    fun initMap(){
        mBaiduMap.isMyLocationEnabled = true

        LocationClient.setAgreePrivacy(true)
        //定位初始化
        mLocationClient = LocationClient(this)

        //通过LocationClientOption设置LocationClient相关参数
        val option = LocationClientOption()
        option.isOpenGps = true // 打开gps

        option.setCoorType("bd09ll") // 设置坐标类型

        option.setScanSpan(1000)

        //设置locationClientOption
        mLocationClient.locOption = option

        //注册LocationListener监听器
        val myLocationListener = MyLocationListener()
        mLocationClient.registerLocationListener(myLocationListener)

        //开启地图定位图层
        mLocationClient.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mBaiduMap.isMyLocationEnabled = false
        mMapView.onDestroy()
        mMapView!!.onDestroy()
    }

    inner class MyLocationListener : BDAbstractLocationListener() {
        override fun onReceiveLocation(location: BDLocation) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return
            }
            val locData = MyLocationData.Builder()
                .accuracy(location.radius) // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(location.direction).latitude(location.latitude)
                .longitude(location.longitude).build()
            mBaiduMap.setMyLocationData(locData)
        }
    }
}